﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineAuction.Business;
using OnlineAuction.Entity;

namespace OnlineAuction.Admin
{
    public partial class ViewUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["UserInfo"] == null
                || Request.Cookies["UserInfo"]["Role"].ToString().CompareTo("Admin") != 0)
            {
                Response.Redirect("../Home.aspx");
            }
            else if (!Page.IsPostBack)
            {
                dataBinded();
            }
        }

        private void dataBinded()
        {
            //Get all User
            UserBO bo = new UserBO();
            List<UserDTO> list = bo.ViewUser(txtSearch.Text);
            cpUser.PageSize = 10;
            cpUser.ShowFirstLast = true;
            cpUser.DataSource = list;
            cpUser.BindToControl = grvUsers;
            grvUsers.DataSource = cpUser.DataSourcePaged;
            grvUsers.DataBind();
        }

        protected void grvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                UserBO bo = new UserBO();
                bo.DeleteUser(e.CommandArgument.ToString());
                dataBinded();
            }
            if (e.CommandName == "Ban")
            {
                UserBO bo = new UserBO();
                bo.BanUser(e.CommandArgument.ToString());
                dataBinded();
            }
            if (e.CommandName == "Active")
            {
                UserBO bo = new UserBO();
                bo.ActiveUser(e.CommandArgument.ToString());
                dataBinded();
            }
        }

        protected void grvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        public String State(String bl)
        {
            String state = "";

            if (bl == "False")
            {
                state = "Baned";
            }
            else
            {
                state = "Actived";
            }
            return state;
        }

        protected void grvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            dataBinded();
        }
    }
}